﻿package com.zjgsu.dao;/*
Date: 2018-04-10 21:51
Description: 
*/

import com.zjgsu.entity.User;
import com.zjgsu.util.JdbcUtil;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class UserDAO2s {
    public User selectUser(String username, String password) {
        String sql = "select * from test_table where username = ? and password = ?";
        //创建填充参数的list
        List<Object> paramList = new ArrayList<Object>();
        TjdbcUtil tjdbcUtil = null;
        try {
            tjdbcUtil = new TjdbcUtil();
            tjdbcUtil.getConnection(); // 获取数据库链接
            List<Map<String, Object>> mapList = tjdbcUtil.findResult(sql.toString(), paramList);
            if(mapList.size()==1){
                Map<String, Object> map = mapList.get(0);
                String name = (String) map.get("name");
                String pwd = (String) map.get("password");
                System.out.println("姓名:"+name+";password:"+password);
                return new User(name,pwd);
            }
        } catch (SQLException e) {
            System.out.println(this.getClass()+"执行查询操作抛出异常！");
            e.printStackTrace();
        } finally {
            if (tjdbcUtil != null) {
                tjdbcUtil.releaseConn(); // 一定要释放资源
            }
        }
        return null;
    }
}
